package com.go.where.manager.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CommonUtil {
	private static final Logger log = Logger.getLogger(CommonUtil.class);
	public static final String FILE_SEPARATOR = "/";

	/**
	 * gson格式转换，日期采用yyyy-MM-dd HH:mm:ss格式转换
	 * 
	 * @return
	 */
	public static Gson gson() {
		GsonBuilder gb = new GsonBuilder();
		gb.setDateFormat("yyyy-MM-dd HH:mm:ss");
		gb.disableHtmlEscaping();
		Gson gson = gb.create();
		return gson;
	}

	/**
	 * 潜复制，对不同对象的相同属性进行赋值，被改变的是des
	 * 
	 * @param source
	 * @param desObject目标对象
	 */
	public static void simpleValueCopy(Object source, Object desObject) {
		if (source == null || desObject == null)
			return;
		Method[] sourceMethods = source.getClass().getMethods();
		Method[] desMethods = desObject.getClass().getMethods();
		for (Method m : sourceMethods) {
			String methodName = m.getName();
			if (methodName.startsWith("get")) {
				String methodTmp = methodName.replaceFirst("get", "set");
				for (Method desMethod : desMethods) {
					try {
						if (methodTmp.equals(desMethod.getName())) {
							desMethod.invoke(desObject, m.invoke(source,
									new Object[] {}));
						}
					} catch (Exception e) {
					}
				}
			} else if (methodName.startsWith("is")) {
				String methodTmp = methodName.replaceFirst("is", "set");
				for (Method desMethod : desMethods) {
					try {
						if (methodTmp.equals(desMethod.getName())) {
							desMethod.invoke(desObject, m.invoke(source,
									new Object[] {}));
						}
					} catch (Exception e) {
					}
				}
			}
		}
	}

	/**
	 * 产生六位随机数，前面兩位是日期其他四位是随机数
	 * 
	 * @return
	 */
	public static String createRandomNumber() {
		Integer random = new Random().nextInt(9999);
		if (random != null && random < 1000) {
			random += 1000;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(new SimpleDateFormat("dd").format(new Date()));
		sb.append(random);
		return sb.toString();
	}

	/**
	 * 产生八位随机数，前面兩位是日期其他四位是随机数
	 * 
	 * @return
	 */
	public static String createRandomNum() {
		Integer random = new Random().nextInt(999999);
		if (random != null && random < 100000) {
			random += 100000;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(new SimpleDateFormat("dd").format(new Date()));
		sb.append(random);
		return sb.toString();
	}

	public static void main(String[] args) {
		int d1 = 1;
		Long d2 = 14L;
		BigDecimal r = div(d1, d2);
		// r = r.multiply(new BigDecimal(100));
		System.out.println(r.doubleValue());

	}

	private static final BigDecimal THOUSANDS = new BigDecimal("1000");

	/**
	 * 对金额进行除1000操作，默认精确到小数点两位
	 * 
	 * @param money金额
	 * @param scale精度
	 *            小數點的位數
	 * 
	 * @return
	 */
	public static String changeMoney(String money, int scale) {
		try {
			BigDecimal b = new BigDecimal(money).divide(THOUSANDS);
			if (b.toString().indexOf(".") > 0) {
				if (scale <= 0)
					b = b.setScale(2, BigDecimal.ROUND_HALF_UP);
				else
					b = b.setScale(scale, BigDecimal.ROUND_HALF_UP);
			}
			return b.toString();
		} catch (Exception e) {
		}
		return null;
	}

	public static int div(Integer d1, Integer d2) { // 进行除法运算
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null || d2 == 0) {
			throw new RuntimeException("除数不能为0");
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		BigDecimal result = b1.divide(b2, BigDecimal.ROUND_HALF_UP);

		return result.intValue();
	}

	/**
	 * 四舍五入，保留四位小数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static BigDecimal div(Integer d1, Long d2) { // 进行除法运算
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null || d2 == 0) {
			throw new RuntimeException("除数不能为0");
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		// BigDecimal result = b1.divide(b2).setScale(4,
		// BigDecimal.ROUND_HALF_UP);
		BigDecimal result = b1.divide(b2, 4, BigDecimal.ROUND_HALF_UP);

		return result;
	}

	/**
	 * 提供准确的加法运算
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int add(Integer d1, Integer d2) { // 进行加法运算
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null) {
			d2 = 0;
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.add(b2).intValue();
	}

	public static int add(Integer d1, Integer d2, Integer d3, Integer d4) { // 进行加法运算
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null) {
			d2 = 0;
		}
		if (d3 == null) {
			d3 = 0;
		}
		if (d4 == null) {
			d4 = 0;
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		BigDecimal b3 = new BigDecimal(d3);
		BigDecimal b4 = new BigDecimal(d4);
		return b1.add(b2).add(b3).add(b4).intValue();
	}

	/**
	 * 比较大小 d1大于d2 返回true
	 * 
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean compare(Integer d1, Integer d2) {
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null) {
			d2 = 0;
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		int re = b1.compareTo(b2);
		if (re > 0) {
			return true;
		} else
			return false;
	}

	/**
	 * 提供准确的减法运算
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int sub(Integer d1, Integer d2) { // 进行加法运算
		if (d1 == null) {
			d1 = 0;
		}
		if (d2 == null) {
			d2 = 0;
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.subtract(b2).intValue();
	}

	/**
	 * 生成html文件
	 * 
	 * @param html
	 * @param filePath
	 */
	public static void createHtml(String html, String filePath) {
		if (StringUtils.isNotBlank(html)) {
			byte[] b;
			try {
				b = html.getBytes("utf-8");
				for (int i = 0; i < b.length; ++i) {
					if (b[i] < 0) {// 调整异常数据
						b[i] += 256;
					}
				}
				OutputStream out = new FileOutputStream(filePath);
				out.write(b);
				out.flush();
				out.close();
			} catch (IOException e) {
				log.error("生成html文件异常", e);
			}
		}
	}
	
	
	public static int getCustomerLevel(Integer score){
		int level = 0;
		if(score != null){
			if(score <1000){
				level = 0;
			}else if(score >= 1000 && score < 2000){
				level = 1;
			}else if(score >= 2000 && score < 4000){
				level = 2;
			}else if(score >= 4000 && score < 8000){
				level = 3;
			}else if(score >= 8000 && score < 16000){
				level = 4;
			}else if(score >= 16000 && score < 32000){
				level = 5;
			}else if(score >= 32000 && score < 64000){
				level = 6;
			}else if(score >= 64000 && score < 128000){
				level = 7;
			}else if(score >= 128000 && score < 256000){
				level = 8;
			}else if(score >= 256000 && score < 512000){
				level = 9;
			}else if(score >= 512000){
				level = 10;
			}
		}
		
		return level;
		
	}
}
